package com.fasteat.mall.controller;

import com.fasteat.common.entity.Result;
import com.fasteat.mall.entity.SearchCondition;
import com.fasteat.mall.service.client.ProductSearchClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 商品搜索前端控制器
 *
 * @author ghost
 * @since 2021-2-8
 */
@RestController
@RequestMapping("/mall/search")
@Api(description = "商品搜索模块")
@CrossOrigin
public class ProductSearchController {

	@Autowired
	private ProductSearchClient productSearchClient;

	@PostMapping
	@ApiOperation("分页高亮默认方式搜索商品")
	public Result pageAndHighlightOfProduct(@RequestBody @ApiParam(value = "搜索条件") SearchCondition condition) {
		if (condition == null
				|| condition.getEntry() == null
				|| condition.getEntry().size() == 0) {
			return Result.error("查询失败");
		}
		return productSearchClient.searchProductWithHighlightAndPage(condition);
	}


	@PostMapping("/{salesVolumeSort}/{priceSort}/{timeSort}")
	@ApiOperation("分页高亮带排序搜索商品")
	public Result pageAndHighlightAndSortOfProduct(@RequestBody @ApiParam(value = "搜索条件") SearchCondition condition,
												   @PathVariable(name = "salesVolumeSort") @ApiParam(required = true, value = "0、默认方式，1、销量升序，2、销量降序") int salesVolumeSort,
												   @PathVariable(name = "priceSort") @ApiParam(required = true, value = "0、默认方式，1、价格升序，2、价格降序") int priceSort,
												   @PathVariable(name = "timeSort") @ApiParam(required = true, value = "0、默认方式，1、时间升序，2、时间降序") int timeSort) {
		if (condition == null
				|| condition.getEntry() == null
				|| condition.getEntry().size() == 0) {
			return Result.error("查询失败");
		}
		return productSearchClient.searchProductWithHighlightAndPageAndSort(
				condition, salesVolumeSort, priceSort, timeSort);
	}


}
